'use strict';
const { Service } = require('egg');
class RouterService extends Service {
  constructor(ctx, modelName) {
    super(ctx);
    this.modelName = modelName;
  }
  pageOrder() {
    return []
  }
  listOrder() {
    return []
  }
  getPageInclude() {
    return []
  }
  getOneInclude() {
    return []
  }
  getPage(where, page, pageSize) {
    return this.ctx.model[this.modelName].findAndCountAll({
      where,
      offset: pageSize * (page - 1),
      limit: pageSize,
      order: this.pageOrder() || [],
      include: this.getPageInclude(),
      distinct: true
    });
  }
  getListInclude() {
    return []
  }
  async allList(where) {
    return this.ctx.model[this.modelName].findAll({
      where,
      order: this.listOrder() || [],
      include: this.getListInclude()
    });
  }
  async findOne(id) {
    console.log('id', id)
    return await this.ctx.model[this.modelName].findOne({
      where: {
        id,
      },
      include: this.getOneInclude()
    });
  }
  async create(data) {
    delete data.id
    return this.ctx.model[this.modelName].create({
      ...data,
    });
  }
  async update(id, data) {
    return await this.ctx.model[this.modelName].update(data, { where: { id } });
  }
  async deleteById(id) {
    return await this.ctx.model[this.modelName].destroy({ where: { id } });
  }
}

module.exports = RouterService;
